package cn.wen.yinxiu.member.service.impl;

import cn.wen.yinxiu.common.enums.DelEnum;
import cn.wen.yinxiu.common.enums.StatusEnum;
import cn.wen.yinxiu.common.to.user.FareTO;
import cn.wen.yinxiu.common.to.user.UserAddressTO;
import cn.wen.yinxiu.common.toolkit.BeanUtil;
import com.alibaba.fastjson.TypeReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.wen.yinxiu.common.toolkit.PageUtils;
import cn.wen.yinxiu.common.toolkit.Query;

import cn.wen.yinxiu.member.dao.AddressDao;
import cn.wen.yinxiu.member.entity.AddressEntity;
import cn.wen.yinxiu.member.service.AddressService;


@Service("addressService")
public class AddressServiceImpl extends ServiceImpl<AddressDao, AddressEntity> implements AddressService {

    @Autowired
    private AddressDao addressDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AddressEntity> page = this.page(
                new Query<AddressEntity>().getPage(params),
                new QueryWrapper<AddressEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<UserAddressTO> getAddressByUserId(Long userId) {
        QueryWrapper<AddressEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("is_delete", DelEnum.NORMAL.code());
        queryWrapper.eq("status", StatusEnum.NORMAL_USE.code());
        List<AddressEntity> addressEntities = addressDao.selectList(queryWrapper);
        if (addressEntities != null && addressEntities.size() != 0) {
            return addressEntities.stream().map(item -> {
                UserAddressTO target = new UserAddressTO();
                BeanUtils.copyProperties(item, target);
                return target;
            }).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    @Override
    public FareTO getAddressFare(Long addrId) {
        FareTO fareTO = new FareTO();

        // 收获地址的详细信息
        AddressEntity addressEntity = addressDao.selectById(addrId);

        if (addressEntity != null) {
            String phone = addressEntity.getReceiverPhone();
            // 截取用户手机号码最后一位作为我们的运费计算
            // 1558022051
            String fare = phone.substring(phone.length() - 10, phone.length()-8);
            BigDecimal bigDecimal = new BigDecimal(fare);

            fareTO.setFare(bigDecimal);
            UserAddressTO target = new UserAddressTO();
            BeanUtils.copyProperties(addressEntity, target);
            fareTO.setAddress(target);

            return fareTO;
        }
        return null;
    }

}